<template>
	<eui-page-frame topHeight="170" bottomHeight="96">
		<template #top>
			<view class="content-img">
				<eui-nav-bar title="填资料" color="#000000" />
			</view>
		</template>
		<eui-page-content>
			<!-- 	<eui-base-card title="填资料"> -->
			<view class="card">
				<view class="talk-content">填资料</view>
				<eui-line></eui-line>
				<eui-card-input v-model="dataModel.c_provider_name" title="意向商户" placeholder="请选择意向商户" type="text"
					displayable="false" rightIconType="right" @iconClick="selectOpen('partner')" required />
				<eui-card-input v-model="dataModel.c_category" :range="privoderType" title="商户类型" rightIconType="right"
					type="selector" placeholder="请选择商户类型" alignType="right" />
				<eui-card-input v-model="dataModel.c_bname" title="意向品牌" placeholder="请选择品牌" type="text"
					displayable="false" rightIconType="right" @iconClick="selectOpen('brand')" />
				<eui-card-input v-model="dataModel.c_counter_cname" title="规划业态" type="text" placeholder=''
					displayable="false" />
				<eui-card-input v-model="dataModel.c_cname" title="品牌品类" type="text" placeholder=''
					displayable="false" />
				<eui-card-input v-model="dataModel.c_talk_place" title="洽谈地点" placeholder="请输入洽谈地点" type="text"
					alignType="right" maxlength="50" />
				<eui-card-input v-model="dataModel.c_con_type" :range="conType" title="经营方式" rightIconType="right"
					type="selector" placeholder="请选择意向经营方式" alignType="right" />
				<eui-card-input v-model="dataModel.c_link_man" title="联系人" placeholder="请选择联系人" type="text"
					maxlength="50" />
				<eui-card-input v-model="dataModel.c_link_man_title" title="职务" placeholder="请输入职务" type="text"
					alignType="right" maxlength="10" />
				<eui-card-input v-model="dataModel.c_link_tele" title="联系电话" placeholder="请输入联系电话" type="text"
					alignType="right" maxlength="100" />
				<eui-card-input v-model="dataModel.c_counter_name" title="意向铺位" placeholder="请选择意向铺位" type="text"
					displayable="false" rightIconType="right" alignType="right" @iconClick="selectOpen('counter')" />
				<eui-card-input v-model="dataModel.c_area_text" title="计租面积" placeholder='' type="text"
					alignType="right" displayable="false" :showBottomLine="false" />
			</view>
			<!-- </eui-base-card> -->
		</eui-page-content>
		<template #bottom>
			<view class="bt-button">
				<eui-button :disabled="!showButtom" type="primary" title="提交" @click="submit" />
			</view>
		</template>
	</eui-page-frame>
</template>


<script setup lang="ts">
	import { ref,computed } from 'vue'
	import { onLoad, onShow } from "@dcloudio/uni-app";
	import { useLoginStore } from '@/store/modules/useLoginStore';
	import { useMainStore } from "@/store/modules/store";
	import useSaveBill from '@/hooks/useSaveBill';
	import { getRelatedName, getDictName, getDictSelectList, getDictSelect } from '../components/RelatedName.js';
	import useService from '@/hooks/useService';
	const service = useService({ prefix: "apiApi" });
	const LoginStore = useLoginStore();
	const mainStore = useMainStore();
	const { loginInfo } = mainStore;
	const billId = ref('');
	
	const conType = getDictSelect({ dataItem: '经营方式', enableDTIds: ['4', '5'] });
	const privoderType = getDictSelectList('供应商类型');
	const { save, createId } = useSaveBill({
		prefix: 'apiApi',
		table: {
			main: [
				{
					tableName: "tb_mo_talk",
					primaryKeys: ["c_id", "c_tenant_id"],
				},
			],
			detail: [
				{ tableName: 'tb_mo_talk_article', primaryKeys: ['c_tenant_id', 'c_id', 'c_article_code'] },
				{ tableName: 'tb_mo_talk_process', primaryKeys: ['c_tenant_id', 'c_id', 'c_process_id'] },
				{ tableName: 'tb_mo_talk_cetificate', primaryKeys: ['c_tenant_id', 'c_id', 'c_name'] },
			]
		},
		uniqueKey: "管易通洽谈记录登记单",
		method: "EnjoyBillSave",
		billName: '洽谈记录登记单',
		billType: 'D11'
	});
	const dataModel = ref({});
	const showButtom =  computed(() => {
		return dataModel.value.c_status==='0' && (dataModel.value.c_approve_status==='1' || !dataModel.value.c_approve_status);
	});
	onLoad(async ({ c_id }) => {
		billId.value = c_id;
		await GetBillItem();
	})
	const selectOpen = (selectType : string) => {
		if (selectType === 'counter') {
			uni.navigateTo({
				url: `../modals/index?type=${selectType}&storeId=${dataModel.value.c_store_id}`
			});
		}
		else {
			uni.navigateTo({
				url: `../modals/index?type=${selectType}`
			});
		}
	}
	const SelectionChanged = async (type, item) => {
		switch (type) {
			case 'partner':
				await ValidPartnerName(item.text);
				//await checkPartner(item.text,dataModel.value.c_store_id);  
				break;
			case 'brand':
				await validBrandName(item.text);
				break
			case 'counter':
				await GetCounterName(item.value);
				break;
		}
	}
	//获取单据详情
	const GetBillItem = async () => {
		const req =
			{
				BillId: billId.value
			};
		//dataModel.value = {};
		const res = await service.request('管易通洽谈记录登记单', 'EnjoyBillQuery', req);
		if (res) {
			dataModel.value = res.ds[0];
			dataModel.value.c_area_text = res.ds[0].c_area != null ? res.ds[0].c_area + 'm²' : '';
			dataModel.value.c_bname = getRelatedName('rdsBrands', dataModel.value.c_bcode);
			dataModel.value.c_counter_name = getRelatedName('rdsCounter', dataModel.value.c_counter_no)
			dataModel.value.c_cname = getRelatedName('rdsGdsclasses', dataModel.value.c_ccode);
			dataModel.value.c_counter_cname = getRelatedName('rdsGdsclasses', dataModel.value.c_counter_ccode);
		}
	}
	//验证供应商名称
	const ValidPartnerName = async (name) => {
		const req = {
			IuputData: name
		};
		const res = await service.request('管易通洽谈记录登记单', 'ValidPartnerName', req);
		if (res) {
			dataModel.value.c_category = res.c_category;
			dataModel.value.c_provider = res.c_code;
			dataModel.value.c_provider_name = res.c_name;
		}
	}
	const validBrandName = async (name) => {
		const req = {
			IuputData: name
		};
		const res = await service.request('管易通洽谈记录登记单', 'ValidBrandName', req);
		if (res) {
			dataModel.value.c_bcode = res.c_code;
			dataModel.value.c_bname = res.c_name
			dataModel.value.c_ccode = res.c_ccode;
			dataModel.value.c_cname = getRelatedName('rdsGdsclasses', res.c_ccode);
		}
	}
	//验证商户编码并获取商户信息
	const checkPartner = async (name, store) => {
		const req = {
			code: name,
			store: store,
		};
		const res = await service.request('管易通洽谈记录登记单', 'PartnerCheck', req);
		if (res) {
			const item = res?.[0];
			console.log('itemitem', item)
			if (dataModel.value.c_id && !item?.c_id) {
				dataModel.value.c_times = 1;
				dataModel.value.c_ref_id = "";
			}
			else if (dataModel.value.c_id && item?.c_id) {
				const c_new_bill = (item.c_status == '3' || item.c_status == '已审核');
				if (c_new_bill) {
					dataModel.value.c_times = item.c_times + 1;
				}
				await initData(item.c_id, c_new_bill);
			}

		}
	}
	const QueryBillData = async (c_id, c_new_bill) => {
		const req = {
			c_id: c_id,
			c_new_bill: c_new_bill ? '1' : '0'
		}
		uni.showLoading({ title: "查询中...", mask: true });
		const res = await service.request('管易通洽谈记录登记单', 'QueryBillData', req);
		uni.hideLoading();
		return res;
	}
	const initData = async (id, c_new_bill = true) => {
		let oldData = dataModel.value
		const result = await QueryBillData(id, c_new_bill);
		if (result) {
			dataModel.value = result.ds[0];
			dataModel.value.c_cname = getRelatedName('rdsGdsclasses', dataModel.value.c_ccode) || '';
			dataModel.value.c_counter_name = getRelatedName('rdsCounter', dataModel.value.c_counter_no) || '';
			dataModel.value.c_counter_cname = getRelatedName('rdsGdsclasses', dataModel.value.c_counter_ccode) || '';
			dataModel.value.c_id = billId.value;
			dataModel.value.c_approve_status = '1';
			dataModel.value.c_bill_type = 'D11';  //单据类型(tb_sc_bill_type中定义)
			dataModel.value.c_status = '0';
			dataModel.value.c_talk_dt = oldData.c_talk_dt;
			dataModel.value.c_mk_store_id = oldData.c_mk_store_id;
			dataModel.value.c_mk_dt = oldData.c_mk_dt;
			dataModel.value.c_mk_userno = oldData.c_mk_userno;
			dataModel.value.c_approve_status = oldData.c_approve_status;
			dataModel.value.c_adno = oldData.c_adno;
			dataModel.value.c_talk_userno = oldData.c_talk_userno;
			dataModel.value.c_talk_type = oldData.c_talk_type;
		}
	}
	//铺位信息
	const GetCounterName = async (code) => {
		const req = {
			IuputData: code,
			//status:'1',   //是否需要这个条件？？？
			storeId: dataModel.value.c_store_id   //当前单据的机构
		};
		const res = await service.request('管易通洽谈记录登记单', 'GetCounterName', req);
		if (res) {
			dataModel.value.c_counter_no = res.c_code;
			dataModel.value.c_area = res.c_use_area;
			dataModel.value.c_area_text = (res.c_use_area != null && res.c_use_area != '') ? res.c_use_area + 'm²' : '';
			dataModel.value.c_counter_name = res.c_name;
			dataModel.value.c_counter_ccode = res.c_bccode;
			dataModel.value.c_counter_cname = getRelatedName('rdsGdsclasses', res.c_bccode);
		}
	}
	//点位信息
	const GetDpointName = async (code) => {
		const req = {
			IuputData: name,
			storeId: dataModel.value.c_store_id
		};
		const res = await service.request('管易通洽谈记录登记单', 'GetDpointName', req);
		if (res) {
			dataModel.value.c_counter_no = res.c_code;
			dataModel.value.c_area = res.c_use_area;
			dataModel.value.c_counter_name = res.c_name;
		}
	}

	const submit = async () => {
		if (!dataModel.value.c_provider_name) {
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '请选择意向商户'
			});
			return;
		}
		if (dataModel.value.c_link_tele) {
			const regx = /(^(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(^[1][35789]\d{9}))$/
			if (!regx.test(dataModel.value.c_link_tele)) {
				uni.showToast({
					duration: 2000,
					icon: 'none',
					title: '请输入正确的电话号码'
				});
				return;
			}
		}
		uni.showLoading({ title: "正在保存...", mask: true })
		const result = await save(false, dataModel.value);
		uni.hideLoading();
		uni.showToast({
			duration: 2000,
			icon: 'none',
			title: '保存成功'
		});
		await GetBillItem();
	}
	defineExpose({
		SelectionChanged
	})
</script>

<style lang="scss">
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #F2F2F2;
		padding: 0px !important
	}

	.content-img {
		background-color: #FFFFFF;
		background-size: 100% 420rpx;
		background-repeat: no-repeat;
		width: 100%;
	}

	.navigation {
		flex: 0 0 auto;
		margin-bottom: 5px;
	}

	.card {
		border-radius: 16rpx;
		//margin: 0px 10px 10px 10px;
		background-color: #ffffff;
	}

	.e-attachment {
		align-items: center;
		justify-content: center;
		display: flex;

		&__image {
			width: 115px;
			height: 115px;
			margin: 6px 6px;
			border-radius: 5px;
		}
	}

	.top-grid {
		background-color: #FFFFFF;
		display: flex;
		flex-direction: row;
		border-radius: 10px;
		margin: 8px 0px;
		padding: 28rpx 0rpx;
		justify-content: space-between;
		justify-items: center;
	}

	.grid-item-box {
		flex: 1;
		// position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;

		&__img {
			width: 88rpx;
			height: 88rpx;
		}

		&__text {
			font-size: 26rpx;
			margin-top: 16rpx;
		}
	}

	.bt-button {
		background-color: #FFFFFF;
		padding: 10rpx 0rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.card {
		border-radius: 20rpx;
		padding:  20rpx 0rpx 10rpx 0rpx;
		margin-bottom: 20rpx;
		background: #ffffff;
	}

	.talk-content {
		font-size: 30rpx;
		color: #000000;
		line-height: 42rpx;
		font-weight: 600;
		padding: 0rpx 28rpx 20rpx 28rpx;
	}

	.talk-content-title {
		font-size: 30rpx;
		color: #000000;
		line-height: 42rpx;
		font-weight: 600;
		margin-bottom: -20rpx;
		padding: 28rpx 0rpx 0rpx 28rpx;
	}
</style>